"""
@author: jiangpj
@email: pj.jiang@hzgosun.com
@file: gridUserManageTest.py
@time: 2019/6/3 9:51

"""
import unittest
from time import sleep

from selenium.webdriver.common.keys import Keys

from common.decorators import log4gs, mkdir_classname
from common.functions import save_screenshot
from common.settings import TAG
from common.vue.select import DropDownSelect, OperateSelect
from pages.gridUserManagePage import GridUserManagePage


@log4gs
@mkdir_classname(TAG)
class GridUserManageTest(unittest.TestCase):
    """网格员管理增删改查功能测试"""

    @classmethod
    def setUpClass(cls):
        cls.log.debug('=' * 30 + '开始网格员管理功能测试' + '=' * 30)
        cls.page = GridUserManagePage('/DataManage/GridUserManage')

    @classmethod
    def tearDownClass(cls):
        cls.log.info('=' * 30 + "结束网格员管理功能测试({})".format(__class__.__name__) + '=' * 30)
        # cls.page.driver.quit()

    def setUp(self):
        self.log.info('>>>>>开始执行用例：{} {}'.format(self.id(), self.shortDescription()))

    def tearDown(self):
        sleep(2)
        self.log.info('<<<<<结束执行用例：{} {}'.format(self.id(), self.shortDescription()))

    @unittest.skip
    def test_1_page(self):
        """网格员管理翻页"""
        sleep(1)
        self.log.info("共有：{}页数据".format(self.page.turnPage.total_page))
        self.log.info("当前处于第：{}页数据".format(self.page.turnPage.current_page))
        self.log.info("共有：{}条数据".format(self.page.turnPage.total_num))
        sleep(2)
        self.assertNotEqual(self.page.turnPage.total_page, 1, msg="页数过少，无法进行翻页测试")
        if int(self.page.turnPage.total_page) >= 3:
            self.page.turnPage.next_page.click()
            self.assertEqual(self.page.turnPage.current_page, '2')
            sleep(2)
            self.page.turnPage.next_page.click()
            self.assertEqual(self.page.turnPage.current_page, '3')
            sleep(2)
            self.page.turnPage.prev_page.click()
            self.assertEqual(self.page.turnPage.current_page, '2')
            sleep(2)
            self.page.turnPage.input.clear()
            self.page.turnPage.input.send_keys(3)
            self.page.turnPage.input.send_keys(Keys.ENTER)
            self.assertEqual(self.page.turnPage.current_page, '3')
        else:
            self.log.error("当前模块最大未到3页，无法进行翻页测试", exc_info=True)
            save_screenshot(self, '翻页功能测试失败')

    @unittest.skip
    def test_2_revamp(self):
        """修改网格员信息"""
        name2 = "123"
        name = "321"
        sleep(1)
        DropDownSelect(self.page.searchTools.keyword_dropdown_list, "网格员姓名")
        self.page.searchTools.keyword_input.send_keys(name)
        self.page.searchTools.search_btn.click()
        sleep(1)
        result = self.page.table.get_row(name)
        OperateSelect(result, "编辑")
        sleep(1)
        self.page.modifyGridUserManage.name.clear()
        sleep(1)
        self.page.modifyGridUserManage.name.send_keys(name2)
        sleep(1)
        self.page.modifyGridUserManage.confirm.click()
        sleep(1)
        self.page.searchTools.keyword_input.clear()
        sleep(1)
        self.page.searchTools.keyword_input.send_keys(name2)
        self.page.searchTools.search_btn.click()
        sleep(1)
        result = self.page.table.get_row(name2)
        self.page.searchTools.keyword_input.clear()
        try:
            self.assertTrue(result, "找到{}".format(name2))
            self.log.info("用例修改网格员信息执行成功")
        except Exception:
            self.log.error("用例修改网格员信息执行失败,", exc_info=True)

    @unittest.skip
    def test_2_2_remove_grid(self):
        """删除网格员信息用例"""
        name = '321'
        self.page.searchTools.keyword_input.send_keys(name)
        sleep(1)
        self.page.searchTools.search_btn.click()
        sleep(1)
        self.page.searchTools.keyword_input.clear()
        sleep(1)
        result = self.page.table.get_row(name)
        OperateSelect(result, "删除")
        sleep(1)
        self.page.searchTools.keyword_input.clear()
        sleep(1)
        rm_result = self.page.table.get_row(name)
        sleep(1)
        try:
            self.assertFalse(rm_result)
            self.log.info(f"用例删除保安执行成功")
        except:
            self.log.error(f"用例删除保安执行失败,", exc_info=True)

    def test_2_1_antistop_search(self):
        """网格员信息关键词搜索"""
        search_list = [
            {
                'condition': "网格名称",
                'input': "河南弃网格",
                'expectation': '河南弃网格'
            },
            {
                'condition': "网格员姓名",
                'input': "自动化测试",
                'expectation': '自动化测试'

            },
        ]
        for item in search_list:
            condition = item.get('condition')
            input_ = item.get('input')
            expectation = item.get('expectation')
            with self.subTest(**item):
                self.log.info("用以下数据进行搜索测试：\n{}".format(item))
                DropDownSelect(self.page.searchTools.keyword_dropdown_list, condition)   # 关键字选择姓名
                self.page.searchTools.keyword_input.clear()
                self.page.searchTools.keyword_input.send_keys(input_)   # 关键字
                self.page.searchTools.search_btn.click()   # 点击检索按钮
                sleep(1)
                result = self.page.table.get_row(expectation)
                self.page.searchTools.keyword_input.clear()
                sleep(1)
                try:
                    self.assertTrue(result, msg="没有找到结果：{}".format(expectation))
                    self.log.info("用例关键词：{}搜索执行成功".format(condition))
                except Exception:
                    self.log.error("用例关键词：{}搜索执行失败,".format(condition), exc_info=True)

    def test_4_gridding(self):
        """网格检索"""
        grid = "江西省-郑州市网格"
        name = "陇陇网格员"
        self.page.searchTools.region_grid_input.click()
        sleep(1)
        self.page.searchTools.region_grid_selector.select(grid)
        sleep(2)
        self.page.searchTools.search_btn.click()  # 点击检索按钮
        sleep(2)
        result = self.page.table.get_row(name)
        try:
            self.assertTrue(result, "通过网格:江西省-郑州市网格查询到了陇陇网格员")
            self.log.info("用例网格：{}网格检索执行成功".format(grid))
        except:
            self.log.info("用例网格：{}网格搜索执行失败".format(grid))


if __name__ == '__main__':
    unittest.main()